
package boundary;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * @version 1.0
 * @date 10/04/2012
 * @author john
 */
public class LoginDialog extends Stage{
    
    private final Group root;
    private final Scene scene;
    private final GridPane grid;
    private final TextField usernameTF;
    private final PasswordField passwordPF;
    private final Button loginB;
    private final Button cancelB;
    private final Button contactB;
    private Label mainL;
    private Label usernameL;
    private Label passwordL;
    
    public LoginDialog(Stage owner){
        super();
        initOwner(owner);
        initModality(Modality.APPLICATION_MODAL);
        root=new Group();
        scene=new Scene(root);
        scene.setFill(Color.WHITE);
        setScene(scene);
        
        grid=new GridPane();
        grid.setPadding(new Insets(20));
        grid.setHgap(10);
        grid.setVgap(6);
        
        mainL=new Label("Ingrese nombre de usuario y contraseña");
        GridPane.setMargin(mainL, new Insets(0, 0, 5, 0));
        usernameL=new Label("Usuario");
        usernameL.setTooltip(new Tooltip("Es el nombre de usuario asociado a la sucursal desde la cual esta accediendo"));
        passwordL=new Label("Contraseña");
        usernameTF=new TextField();
        passwordPF=new PasswordField();
        loginB=new Button("Aceptar");
        loginB.setOnAction(loginAction());
        cancelB=new Button("Cancelar");
        cancelB.setTooltip(new Tooltip("Si no inicia sesión no podra continuar"));
        cancelB.setOnAction(cancelAction());
        HBox buttonsHBox=new HBox(7);
        GridPane.setHalignment(buttonsHBox, HPos.RIGHT);
        GridPane.setMargin(buttonsHBox, new Insets(2, 0, 0, 0));
        buttonsHBox.getChildren().addAll(loginB,cancelB);
        Separator afterButtonsSeparator=new Separator();
        GridPane.setMargin(afterButtonsSeparator, new Insets(3, 0, 3, 0));
        contactB=new Button("¿Problemas para conectarse?");
        contactB.setOnAction(contactAction());
        GridPane.setHalignment(contactB, HPos.RIGHT);
       
        grid.add(mainL, 0, 0, 2, 1);
        grid.add(usernameL, 0, 1);
        grid.add(passwordL, 0, 2);
        grid.add(usernameTF, 1, 1);
        grid.add(passwordPF, 1, 2);
        grid.add(buttonsHBox, 1, 3);
        grid.add(afterButtonsSeparator,0,4,2,1);
        grid.add(contactB, 1, 5);
        root.getChildren().add(grid);
        setTitle("Iniciar sesión");
        setOpacity(0.9);
        setResizable(false);
        setOnCloseRequest(cosumeCloseRequest());
    }
    
    /**
     * 
     */
    public void showStage(){
        show();
        usernameTF.requestFocus();
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> loginAction(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // Validar entradas e iniciar sesion
                System.err.println("Inicio de sesion no implementado.");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> cancelAction(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Terminando...");
                Platform.exit();
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> contactAction(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MainWindow.closeLoginDialog();
                MainWindow.showContactDialog();
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<WindowEvent> cosumeCloseRequest(){
        return new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                event.consume();
            }
        };
    }
    
}